/**
 * *************************************
 * Copyright(c)2016 Dingdang Yixia 
 * All Rights Reserved.
 *
 * @File: CustomerControlller.java
 * @Package: com.ddyx.rest
 * @Description:
 * 
 * @Version: V1.0
 * @Author: Yidong.Xiang
 * @Date: 2017年8月24日 上午11:33:21
 * 
 * *************************************
 */
package com.ddyx.rest;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ddyx.service.ICustomerService;
import com.ddyx.service.IDealDetailsService;
import com.ddyx.service.IDealService;

/**
 * @ClassName: CustomerControlller.java
 * @Module: 客户管理web模块
 * @Description: 
 * 
 * @author: Yidong.Xiang
 * @date: 2017年8月24日 上午11:33:21
 * 
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Resource
	ICustomerService customerService;
	@Resource
	IDealService iDealService;
	@Resource
	IDealDetailsService IDealDetailsService;
	
	/**
	 * 查看VIP顾客列表
	 * @param page
	 * @param size
	 * @param filter
	 * @return
	 */
	@RequestMapping(params = "cmd=listVip", method = RequestMethod.GET)
	public @ResponseBody String listVip(@RequestParam("page") int page
			,@RequestParam("size") int size
			,@RequestParam(value="filter",required=false) String filter)
	{
		return customerService.listVip(page, size, filter);
	}
	/**
	 * 查看普通顾客列表
	 * @param page
	 * @param size
	 * @param filter
	 * @return
	 */
	@RequestMapping(params = "cmd=listGuest", method = RequestMethod.GET)
	public @ResponseBody String listGuest(@RequestParam("page") int page
			,@RequestParam("size") int size
			,@RequestParam(value="filter",required=false) String filter)
	{
		return customerService.listGuest(page, size, filter);
	}
	/**
	 * 新增顾客
	 * @param customerName
	 * @param nickname
	 * @param gender
	 * @param email
	 * @param mobile
	 * @param address
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST, consumes = "application/json")
	public @ResponseBody int addCustomer(@RequestBody JSONObject customer
//			@RequestParam("faceId") int faceId
//			,@RequestParam("customerName") String customerName
//			,@RequestParam(value="nickname",required=false) String nickname
//			,@RequestParam("gender") String gender
//			,@RequestParam(value="email",required=false) String email
//			,@RequestParam("mobile") String mobile
//			,@RequestParam(value="address",required=false) String address
			)
	{
		
		int faceId = customer.getIntValue("faceId");
		String customerName = customer.getString("customerName");
		String nickname = customer.getString("nickname");
		String gender = customer.getString("gender");
		String email = customer.getString("email");
		String mobile = customer.getString("mobile");
		String address = customer.getString("address");
		int isVip = customer.getInteger("isVip");
		String contactId = customer.getString("contactId");
		String tags = customer.getString("tags");
		return customerService.addCustomer(faceId,customerName, nickname, gender, email, mobile, address,isVip,contactId);
	}

//	/**
//	 * 修改顾客基本信息
//	 * @param customerName
//	 * @param nickname
//	 * @param gender
//	 * @param email
//	 * @param mobile
//	 * @param address
//	 * @return
//	 */
//	@RequestMapping(params = "cmd=editCustomer", method = RequestMethod.POST)
//	public @ResponseBody int editCustomer(@RequestParam("customerId") int customerId
//			,@RequestParam(value="customerName",required=false) String customerName
//			,@RequestParam(value="nickname",required=false) String nickname
//			,@RequestParam(value="gender",required=false) String gender
//			,@RequestParam(value="email",required=false) String email
//			,@RequestParam(value="mobile",required=false) String mobile
//			,@RequestParam(value="address",required=false) String address
//			,@RequestParam(value="isvip",required=false) int isVip)
//	{
//		
//		return customerService.editCustomer(customerId, customerName, nickname, gender, email, mobile, address,isVip);
//	}
	
	/**
	 * 修改顾客基本信息
	 * @param customerName
	 * @param nickname
	 * @param gender
	 * @param email
	 * @param mobile
	 * @param address
	 * @return
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST, consumes = "application/json")
	public @ResponseBody int editCust(@RequestBody JSONObject customer)
	{
//		int customerId = customer.getIntValue("customerId");
//		String customerName = customer.getString("customerName");
//		String nickname = customer.getString("nickname");
//		String gender = customer.getString("gender");
//		String email = customer.getString("email");
//		String mobile = customer.getString("mobile");
//		String address = customer.getString("address");
//		int isVip = customer.getInteger("isVip");
//		int faceId = customer.getInteger("faceId");
//		String contactId = customer.getString("contactId");
		return customerService.editCustomer(customer);
	}
	/**
	 * 获取顾客基本信息
	 * @param faceId
	 * @return
	 */
	@RequestMapping(params = "cmd=getCustomer", method = RequestMethod.GET)
	public @ResponseBody String getCustomer(@RequestParam("contactId") String contactId)
	{
		
		return customerService.getCustomer(contactId);
	}

	/**
	 * 获取顾客详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "cmd=getDocument", method = RequestMethod.GET)
	public @ResponseBody String getDocument(@RequestParam("faceId") int faceId)
	{
		
		return"";
	}
	/**
	 * 查看顾客交易记录
	 * @param page
	 * @param size
	 * @param customerId
	 * @return
	 */
	@RequestMapping(params = "cmd=listDeal", method = RequestMethod.GET)
	public @ResponseBody String listDeal(@RequestParam("page") int page
			,@RequestParam("size") int size
			,@RequestParam("customerId") int customerId)
	{
		return customerService.listDeal(page, size, customerId);
	}
	
	/**
	 * 查看顾客进店事件
	 * @param page
	 * @param size
	 * @param faceId
	 * @return
	 */
	@RequestMapping(params = "cmd=listEvent", method = RequestMethod.GET)
	public @ResponseBody String listEvent(@RequestParam("page") int page
			,@RequestParam("size") int size
			,@RequestParam("faceId") int faceId)
	{
		return customerService.listEvent(page, size, faceId);
	}
	/**
	 * 查找门店交易记录
	 * @param page
	 * @param size
	 * @param vipNo
	 * @param mobile
	 * @param billNo
	 * @return
	 */
	@RequestMapping(params = "cmd=findDeal", method = RequestMethod.GET)
	public @ResponseBody String findDeal(@RequestParam("page") int page
			,@RequestParam("size") int size
			,@RequestParam("vipNo") String vipNo
			,@RequestParam("mobile") String mobile
			,@RequestParam("billNo") String billNo
			)
	{
		return customerService.listDeal(page, size, 1);
	}	
	/**
	 * 顾客交易关联
	 * @param customerId
	 * @param billNo
	 * @param storeId
	 * @return
	 */
	/*@RequestMapping(params = "cmd=setCustDeal", method = RequestMethod.POST)
	public @ResponseBody String setDeal(@RequestParam("customerId") int customerId
			,@RequestParam(value="mobile",required=false) String mobile
			,@RequestParam(value="billno",required=false) String billno
			,@RequestParam(value="vipNo",required=false) String vipNo)
	{
		return"";
	}*/
	
	@RequestMapping(params = "cmd=setDeal", method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> setDeal(@RequestBody JSONObject deal) {
		int customerId = deal.getIntValue("customerId");
		String  billNo = deal.getString("billNo");
		int storeId = deal.getIntValue("storeId");
		/*Deal deal2 = iDealService.selectDealByMasterId(billNo, customerId, storeId);
		try {
			iDealService.insert(deal2);
			int i = deal2.getDealId();
			List<DealDetails> list = IDealDetailsService.selectByDealId(i,billNo);
			IDealDetailsService.insert(list);
			iDealService.updateStatus(billNo);
			map.put("status", true);
			map.put("data", list);
			return map;
		}catch(Exception e){
			map.put("status", false);
			map.put("data", new JSONArray());
			return map;
		}*/
		Map<String, Object> map = customerService.setDeal(billNo, customerId, storeId);
		return map;
	}
	/**
	 * 查看VIP顾客列表
	 * @param page
	 * @param size
	 * @param filter
	 * @return
	 */
	@RequestMapping(value = "findByCustName", method = RequestMethod.GET)
	public @ResponseBody String findById(@RequestParam("page") int page
			,@RequestParam("size") int size
			,@RequestParam(value="customerName",required=false) String filter)
	{
		return customerService.listVip(page, size, filter);
	}
}
